/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cl.innovision.helper.utils;

import cl.innovision.vo.Cursor;
import cl.innovision.vo.InParameters;
import cl.innovision.vo.OutParameters;
import cl.innovision.vo.Parameter;
import cl.innovision.vo.Procedure;
import cl.innovision.vo.Procedures;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author PATO
 */
public class ConnectionDB {

    private static Connection conn = null;
    private static ConnectionDB singleton = null;

    private static String db;
    private String host;
    private int puerto;
    private String instancia;
    private String esquema;
    private String usuario;
    private String contrasena;
    private String dataBase;
    
    public String getDb() {
        return db;
    }

    public String getEsquema() {
        return esquema;
    }
    
    public static ConnectionDB getInstance(String db, String host, int puerto, String instancia, String esquema, String usuario, String contrasena, String dataBase) throws Exception {
        if (singleton == null) {
            singleton = new ConnectionDB( db,  host,  puerto,  instancia,  esquema,  usuario,  contrasena, dataBase) ;
        }
        return singleton;
    }
    
    public static ConnectionDB getInstance() throws Exception {
        
        return singleton;
    }
    
     public static Connection getConexion() throws Exception {
        
        return conn;
    }
    public ConnectionDB(String db, String host, int puerto, String instancia, String esquema, String usuario, String contrasena, String dataBase) throws Exception {
        this.db = db;
        this.host = host;
        this.puerto = puerto;
        this.instancia = instancia;
        this.esquema = esquema;
        this.usuario = usuario;
        this.contrasena = contrasena;
        this.dataBase = dataBase;
        getConnection();
       
    }
    public static String getNombreTipoBD()
    {
        return db;
    }

    private  Connection getConnection() throws Exception {

        if (db.equalsIgnoreCase("oracle")) {
            try {
                Class.forName("oracle.jdbc.driver.OracleDriver");
            } catch (ClassNotFoundException e) {
                throw new Exception("No pudo cargar driver jdbc " + "oracle.jdbc.driver.OracleDriver", e);

            }

            try {
                conn = DriverManager.getConnection("jdbc:oracle:thin:@" + this.host + ":" + this.puerto + ":" + this.instancia, this.usuario, this.contrasena);
            } catch (SQLException e) {
                throw new Exception("No pudo conectar a base de datos: " + e.getMessage());
            }
            return conn;
        } else if (db.equalsIgnoreCase("mysql")) {
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {

                throw new Exception("No pudo cargar driver jdbc " + "oracle.jdbc.driver.OracleDriver: " + e.getMessage());

            }
            try {
                conn = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.puerto + "/" + this.esquema, this.usuario, this.contrasena);
            } catch (SQLException e) {
                throw new Exception("No pudo conectar a base de datos: " + e.getMessage());
            }
            return conn;
        } else if (db.equalsIgnoreCase("sqlserver")) {
           
            try {
                  
                if (this.instancia.equals("") || this.instancia==null)
                {
                     try {
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            } catch (ClassNotFoundException e) {
                throw new Exception("No pudo cargar driver jdbc " + "com.microsoft.sqlserver.jdbc.SQLServerDriver", e);
            }
                    
                    
                    
                    String url= "jdbc:sqlserver://"+this.host+":"+this.puerto+";database="+this.dataBase+";user="+this.usuario+";password="+this.contrasena+";encrypt=true;hostNameInCertificate=*.database.windows.net;loginTimeout=30;";
                    conn = DriverManager.getConnection(url);
                }
                else
                {
                     try {
               Class.forName("net.sourceforge.jtds.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                throw new Exception("No pudo cargar driver jdbc " + "com.microsoft.sqlserver.jdbc.SQLServerDriver", e);
            }
                     
                     conn = DriverManager.getConnection("jdbc:jtds:sqlserver://"+this.host+";instance="+this.instancia+";DatabaseName="+this.esquema, this.usuario, this.contrasena);
                }
                   
                    //conn = DriverManager.getConnection("jdbc:jtds:sqlserver://"+this.host+";instance="+this.instancia+";DatabaseName="+this.esquema, this.usuario, this.contrasena);
                
                //conn = DriverManager.getConnection("jdbc:sqlserver://"+this.host+";instance="+this.instancia+";DatabaseName="+this.esquema, this.usuario, this.contrasena);
                //conn = DriverManager.getConnection("jdbc:sqlserver://INNOVISION-PC/SQLEXPRESS;databaseName=concentrador_productos;integratedSecurity=true");
                //conn = DriverManager.getConnection("jdbc:sqlserver:" + this.host + ":" + this.puerto + ":" + this.instancia, this.usuario, this.contrasena);
            } catch (SQLException e) {
                throw new Exception("No pudo conectar a base de datos "+e.getMessage());
            }
            return conn;
        } else if (db.equalsIgnoreCase("postgresql")) {
            try {
                Class.forName("org.postgresql.Driver");
            } catch (ClassNotFoundException e) {
                throw new Exception("No pudo cargar driver jdbc " + "oracle.jdbc.driver.OracleDriver", e);
            }
            try {
                conn = DriverManager.getConnection("jdbc:postgresql:" + this.host + ":" + this.puerto + ":" + this.instancia, this.usuario, this.contrasena);
            } catch (SQLException e) {
                throw new Exception("No pudo conectar a base de datos ", e);
            }
            return conn;
        } else {
            return null;
        }
        

    }

}
